﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DynReport.aspx.cs" Inherits="DynReport" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
   <script src="../../Scripts/CreateControl.js" type="text/javascript"></script>
   <script src="../../Scripts/GRUtility.js" type="text/javascript"></script>
   <script type="text/javascript">
    var Report;
    var rData;
    function SetData(cols, data, title) {
        rData = data;
        Report = ReportViewer.Report;
        Report.Clear();
        Report.Font.Point = 9;

        var Reportheader = Report.InsertReportHeader();
        Reportheader.Height = 1.38;

        //插入一个静态文本框,显示报表标题文字
        var StaticBox = Reportheader.Controls.Add(1).AsStaticBox; //GRControlType.grctStaticBox
        StaticBox.Text = title;
        StaticBox.Center = 1; //GRCenterStyle.grcsHorizontal 使部件框在节中水平方向上居中对齐
        StaticBox.Font.Point = 15;
        StaticBox.Font.Bold = true;
        StaticBox.Top = 0.40;
        StaticBox.Width = 5.64;
        StaticBox.Height = 0.58;

        DefineDetailGrid(cols, data);
        //var DataURL = encodeURI("/Common/DynReportData.aspx?Data=" + data);
        //ReportViewer.DataURL = DataURL;

        // ReportViewer.Report.LoadDataFromURL("/Common/DynReportData.aspx?Data=" + data);
        ReportViewer.Start();
     }

    function SetDataWithTH(cols, data, title,titleHeight) {
        rData = data;
        Report = ReportViewer.Report;
        Report.Clear();
        Report.Font.Point = 9;
        
        //var Reportheader = Report.InsertReportHeader();
        //Reportheader.Height = 1.38;

        //插入一个静态文本框,显示报表标题文字
        //var StaticBox = Reportheader.Controls.Add(1).AsStaticBox; //GRControlType.grctStaticBox
        //StaticBox.Text = title;
        //StaticBox.Center = 1; //GRCenterStyle.grcsHorizontal 使部件框在节中水平方向上居中对齐
        //StaticBox.Font.Point = 15;
        //StaticBox.Font.Bold = true;
        //StaticBox.Top = 0.40;
        //if (title.length > 15) {
        //    StaticBox.Width = 8;
        //}
        //else {
        //    StaticBox.Width = 5.64;
        //}
        //StaticBox.Height = 0.58;

        DefineDetailGridWithTH(cols, data, titleHeight);
        //var DataURL = encodeURI("/Common/DynReportData.aspx?Data=" + data);
        //ReportViewer.DataURL = DataURL;

        // ReportViewer.Report.LoadDataFromURL("/Common/DynReportData.aspx?Data=" + data);
        ReportViewer.Start();
    }

    function DefineDetailGridWithTH(cols, data, titleHeight) {
        Report.InsertDetailGrid();
        Report.DetailGrid.ColumnTitle.Height = titleHeight;
        Report.DetailGrid.ColumnContent.Height = 0.58;
        //定义数据集的各个字段
        var RecordSet = Report.DetailGrid.Recordset;

        var cnt = cols.length;
        for (var i = 0; i < cnt; i++) {
            RecordSet.AddField(cols[i].field, 1);
            Report.DetailGrid.AddColumn(cols[i].field, cols[i].header, cols[i].field, cols[i].width);
        }

        Report.PrepareRecordset();

        var dataCnt = data.length;
        for (var i = 0; i < dataCnt; i++) {
            RecordSet.Append();
            for (var j = 0; j < cnt; j++) {
                var mDate = data[i][cols[j].field];
                mDate = dateToStr(mDate);

                if (mDate != "") {
                    //日期型的数据
                    Report.FieldByName(cols[j].field).Value = mDate;
                }
                else {
                    Report.FieldByName(cols[j].field).Value = data[i][cols[j].field];
                }
            }
            RecordSet.Post();
        }
    }

    function window_onload() {
        //        Report = ReportViewer.Report;
        //        Report.Clear();

        //        //定义报表主对象的属性
        //        Report.Font.Point = 9;
        //        //定义明细网格
        //       // DefineDetailGrid();

        //        //启动报表运行
        //        ReportViewer.Start();
    }

    //将date型转换为tring 
    //传来的datetime是:Wed Mar 04 2009 11:05:05 GMT+0800格式  得到结果：2009-06-12 17:18:05
    function dateToStr(datetime) {
        var isDate = Date.parse(datetime);
        if (isNaN(isDate)) {
            return "";
        }
        var time ="";
        try 
        {            
            var year = datetime.getFullYear();
            var month = datetime.getMonth() + 1;//js从0开始取 
            var date = datetime.getDate();
            var hour = datetime.getHours();
            var minutes = datetime.getMinutes();
            var second = datetime.getSeconds();

            if (month < 10) {
                month = "0" + month;
            }
            if (date < 10) {
                date = "0" + date;
            }
            if (hour < 10) {
                hour = "0" + hour;
            }
            if (minutes < 10) {
                minutes = "0" + minutes;
            }
            if (second < 10) {
                second = "0" + second;
            }

            time = year + "-" + month + "-" + date + " " + hour + ":" + minutes + ":" + second; //2009-06-12 17:18:05
        } 
        catch (err)
        {
           
        }
        return time;
    }
       
    function DefineDetailGrid(cols, data) {
        
        Report.InsertDetailGrid();
        Report.DetailGrid.ColumnTitle.Height = 0.58 * 2;
        Report.DetailGrid.ColumnContent.Height = 0.58;
        //Red = 255, Green = 196, Blue = 196
        //Report.DetailGrid.ColumnContent.AlternatingBackColor = ColorFromRGB(196, 196, 255);
        //定义数据集的各个字段
        var RecordSet = Report.DetailGrid.Recordset;
        
        var cnt = cols.length;
        for (var i = 0; i < cnt; i++) {
            if (cols[i].field != undefined) {
                RecordSet.AddField(cols[i].field, 1);
                Report.DetailGrid.AddColumn(cols[i].field, cols[i].header, cols[i].field, 2.78);
            }
        }

        Report.PrepareRecordset();

        var dataCnt = data.length;
        for (var i = 0; i < dataCnt; i++) {
            RecordSet.Append();
            for (var j = 0; j < cnt; j++) {
                if (data[i][cols[j].field] != undefined) {
                    var mDate = data[i][cols[j].field];
                    mDate = dateToStr(mDate);
                    if (mDate != "") {
                        //日期型的数据
                        Report.FieldByName(cols[j].field).Value = mDate;
                    }
                    else {
                        Report.FieldByName(cols[j].field).Value = data[i][cols[j].field];
                    }
                }
            }
            //Recordset.FieldByName()
            //  Report.FieldByName("stmc").Value = "aaa";
            //            fldC1.AsString = "One";
            //            fldI1.AsInteger = 321;
            //            fldF1.AsFloat = 123.45;
            RecordSet.Post();
        }
    }

   </script>
     <style type="text/css">
        html,body {
            margin:0;
            height:100%;
        }
    </style>
</head>
<body style="margin:0" onload="return window_onload()">
	<script type="text/javascript">
        
	    CreatePrintViewerEx("100%", "100%", "", "", true, "");
	</script>
</body>
</html>
